public class Demo2 {
    public static int count = 0;
    public static Object locker = new Object();
    public static void main4(String[] args) {
        Thread t1 = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                synchronized (locker){
                    while(count % 3 != 0){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("A");
                    count++;
                    locker.notifyAll();
                }

            }
        });
        Thread t2 = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                synchronized (locker){
                    while(count % 3 != 1){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.print("B");
                    count++;
                    locker.notifyAll();
                }
            }
        });
        Thread t3 = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                synchronized (locker){
                    while(count % 3 != 2){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    System.out.println("C");
                    count++;
                    locker.notifyAll();
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();
    }
    public static void main3(String[] args) {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Object locker3 = new Object();
        Thread t1 = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                synchronized (locker1){
                    try {
                        locker1.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    synchronized (locker2){
                        System.out.print("A");
                        locker2.notify();
                    }
                }
            }
        });
        Thread t2 = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                synchronized (locker2){
                    try {
                        locker2.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    synchronized (locker3){
                        System.out.print("B");
                        locker3.notify();
                    }
                }
            }
        });
        Thread t3 = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                synchronized (locker3){
                    try {
                        locker3.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    synchronized (locker1){
                        System.out.println("C");
                        locker1.notify();
                    }
                }
            }
        });
        t1.start();
        t2.start();
        t3.start();
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        synchronized (locker1){
            locker1.notify();
        }
    }
    public static void main(String[] args) {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Object locker3 = new Object();
        Thread t1 = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                synchronized(locker1){
                    try {
                        locker1.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print("A");
                synchronized (locker2){
                    locker2.notify();
                }
            }
        });
        Thread t2 = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                synchronized (locker2){
                    try {
                        locker2.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.print("B");
                synchronized (locker3){
                    locker3.notify();
                }
            }
        });
        Thread t3 = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                synchronized (locker3){
                    try {
                        locker3.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("C");
                synchronized (locker1){
                    locker1.notify();
                }
            }
        });

        t1.start();
        t2.start();
        t3.start();
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        synchronized (locker1){
            locker1.notify();
        }
    }
    public static void main1(String[] args) throws InterruptedException {
        Object locker1 = new Object();
        Object locker2 = new Object();
        Object locker3 = new Object();
        Thread t1 = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                synchronized(locker2){
                    synchronized (locker1){
                        System.out.print("A");
                    }
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread t2 = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                synchronized(locker3){
                    synchronized(locker2){
                        System.out.print("B");
                    }
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread t3 = new Thread(() -> {
            for(int i = 0; i < 10; i++){
                synchronized(locker3){
                    synchronized (locker1)
                    {
                        System.out.println("C");
                    }
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        t1.start();
        Thread.sleep(10);
        t2.start();
        Thread.sleep(10);
        t3.start();
        Thread.sleep(10);

        t1.join();
        t2.join();
        t3.join();
    }
}
